package com.bugman.validators;

import com.bugman.managed.BaseBean;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class PasswordValidator extends BaseBean implements Validator {

    private String password;
    private String passCheck;
    final private String ATTRIBUTE_PASSWORD_ID = "passwordId";
    private ResourceBundle rb = getBundle();

    public void validate(FacesContext context, UIComponent component, Object value)
            throws ValidatorException {

        String passwordId = (String) component.getAttributes().get(ATTRIBUTE_PASSWORD_ID);
        UIInput passwordInput = (UIInput) context.getViewRoot().findComponent(passwordId);
        
        password = (String) passwordInput.getValue();
        passCheck = (String) value;

        if (password == null || !password.equals(passCheck)) {
            throw new ValidatorException(new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    rb.getString("validatorPassword_error"),
                    rb.getString("validatorPassword_error_detail")));
        }
    }
}